package com.edu.zhku.wms.service;

import cn.hutool.core.date.DateUtil;
import com.edu.zhku.wms.dto.WageQueryDto;
import com.edu.zhku.wms.mapper.EmployeeMapper;
import com.edu.zhku.wms.mapper.WageMapper;
import com.edu.zhku.wms.pojo.Employee;
import com.edu.zhku.wms.pojo.Wage;
import com.edu.zhku.wms.pojo.WageExample;
import com.edu.zhku.wms.vo.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class WageServiceImpl implements WageService {

    @Autowired
    WageMapper wageMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    UserPrincipalService userPrincipalService;

    @Override
    public int addWage(Wage wage) {
        Employee employee = employeeMapper.selectByPrimaryKey(wage.getEmployeeId());
        if (employee == null) {
            return 0;
        }
        Date entryDate = employee.getEntryDate();
        int entryYear = DateUtil.year(entryDate);
        int entryMonth = DateUtil.month(entryDate) + 1;
        // 不会在入职之前有工资
        // 如果 工资年份比入职年份早 或者 相同年份但工资月份比入职月份早 则录入失败
        if ((wage.getYear() < entryYear) || (wage.getYear() == entryYear && wage.getMonth() < entryMonth)) {
            return 0;
        }

        WageExample wageExample = new WageExample();
        wageExample.createCriteria()
                .andEmployeeIdEqualTo(employee.getId())
                .andYearEqualTo(wage.getYear())
                .andMonthEqualTo(wage.getMonth());
        // 相同年月一个人至多只能有一份工资
        if (wageMapper.countByExample(wageExample) > 0) {
            return 0;
        }

        return wageMapper.insert(wage);
    }

    @Override
    public int updateWage(Wage wage) {

        Wage wageInDB = wageMapper.selectByPrimaryKey(wage.getId());
        // 不应该修改一份工资的年月和员工，而是只修改数值
        wage.setYear(wageInDB.getYear());
        wage.setMonth(wageInDB.getMonth());
        wage.setEmployeeId(wageInDB.getEmployeeId());

        return wageMapper.updateByPrimaryKey(wage);
    }

    @Override
    public Wage getWageById(Integer id) {
        return wageMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Wage> wageList(WageQueryDto wageQueryDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        WageExample wageExample = new WageExample();
        WageExample.Criteria criteria = wageExample.createCriteria();
        if (wageQueryDto.getEmployeeId() != null) {
            criteria.andEmployeeIdEqualTo(wageQueryDto.getEmployeeId());
        }
        if (wageQueryDto.getYear() != null) {
            criteria.andYearEqualTo(wageQueryDto.getYear());
//            if (wageQueryDto.getMonth() != null) {
//                criteria.andMonthEqualTo(wageQueryDto.getMonth());
//            }
        }
        return new PageInfo<>(wageMapper.selectByExample(wageExample));
    }

    @Override
    public int deleteWageById(Integer id) {
        return wageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Wage> getCurrentWageList(HttpServletRequest request, Integer year, Integer pageNum, Integer pageSize) {
        UserInfo userInfo = userPrincipalService.getLoggedInfo(request);
        if (userInfo == null || userInfo.getEmployee() == null) {
            return null;
        }
        WageQueryDto wageQueryDto = new WageQueryDto();
        wageQueryDto.setEmployeeId(userInfo.getEmployee().getId());
        if (year != null) {
            wageQueryDto.setYear(year);
        }
        return wageList(wageQueryDto, pageNum, pageSize);
    }
}
